package cn.cupk.corporateshuttlemanagesystem.controller;

import cn.cupk.corporateshuttlemanagesystem.mapper.BusesMapper;
import cn.cupk.corporateshuttlemanagesystem.pojo.Buses;
import cn.cupk.corporateshuttlemanagesystem.result.PageResult;
import cn.cupk.corporateshuttlemanagesystem.result.RequestParams;
import cn.cupk.corporateshuttlemanagesystem.result.Result;
import cn.cupk.corporateshuttlemanagesystem.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 班车管理控制器，处理与班车相关的HTTP请求
 */
@RestController
@RequestMapping("/clxx")
@Slf4j
public class BusesController {

    @Autowired
    private BusesMapper busesMapper;

    @Autowired
    private UserService userService;

    /**
     * 分页查询班车列表
     *
     * @param params 请求参数，包含分页信息和查询条件
     * @return 分页查询结果
     */
    @PostMapping("/list")
    public Result<Object> page(@RequestBody RequestParams<Buses> params) {
        // 分页
        Page<Object> page = PageHelper.startPage(params.getPageNum(), params.getPageSize());
        // 查询
        List<Buses> buses = busesMapper.page(params.getData());
        // 返回分页结果
        return Result.ok(new PageResult<>(page.getTotal(), buses));
    }

    /**
     * 添加班车信息
     *
     * @param buses 新增的班车信息
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result<Object> add(@RequestBody Buses buses) {
        // 校验班车是否已存在
        Buses existingBus = busesMapper.selectByBusId(buses.getLicensePlate());
        if (existingBus != null) {
            return Result.error("车牌号已存在");
        }
        // 校验公司是否存在
        if (userService.findCompanyById(buses.getCompanyId()) == null) {
            return Result.error("该公司未加入本系统");
        }
        // 添加班车
        Integer add = busesMapper.add(buses);
        return Result.ok(add > 0 ? "添加成功" : "添加失败");
    }

    /**
     * 修改班车信息
     *
     * @param buses 更新后的班车信息
     * @return 修改结果
     */
    @PostMapping("/update")
    public Result<Object> update(@RequestBody Buses buses) {
        // 校验ID不能为空
        Assert.notNull(buses.getBusId(), "id不能为空");
        // 校验车牌号是否已存在（排除当前修改的班车）
        Buses existingBus = busesMapper.selectByBusId(buses.getLicensePlate());
        if (existingBus != null && !existingBus.getBusId().equals(buses.getBusId())) {
            return Result.error("车牌号已存在");
        }
        // 校验公司是否存在
        if (userService.findCompanyById(buses.getCompanyId()) == null) {
            return Result.error("该公司未加入本系统");
        }
        // 更新班车信息
        Integer update = busesMapper.update(buses);
        return Result.ok(update > 0 ? "修改成功" : "修改失败");
    }


}
